package com.koron.plan.web.repairplan.controller;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.plan.config.Constants;
import com.koron.plan.web.maintainplan.bean.vo.MaintainPlanDetailVO;
import com.koron.plan.web.maintainplan.mapper.MaintainPlanMapper;
import com.koron.plan.web.maintainplan.service.MaintainPlanService;
import com.koron.plan.web.repairplan.service.PlanPackageIssueService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@Api("试验计划")
@RequestMapping("experiment")
public class ExperimentController implements WorkflowController {


    @Autowired
    private MaintainPlanService maintainPlanService;

    @Autowired
    private PlanPackageIssueService issueService;
    @Autowired
    private HttpServletRequest httpServletRequest;

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Override
    public Response<Object> updateState(RedirectQuery redirectQuery) {
        return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), factory-> {
            MaintainPlanMapper mapper = factory.getMapper(MaintainPlanMapper.class);
            Integer operation = redirectQuery.getOperation();
            MaintainPlanDetailVO vo = JSON.parseObject(redirectQuery.getFormVariables(), MaintainPlanDetailVO.class);
            switch (operation) {
                case 256: // 撤回
                case 1: // 驳回
                    // 驳回的节点编号
                    // 驳回到草稿节点
                    vo.setStatus(Constants.MAINTAIN_PLAN_STATE.DRAFT);
                    break;
                case 32: // 同意
                    vo.setStatus(Constants.MAINTAIN_PLAN_STATE.WAIT_REVIEW);
                    break;
            }
            vo.setDeleteFlag(false);
            boolean flag = mapper.updateById(vo);
            return Response.succ(flag);
        },Response.class);
    }

    /**
     * 流程结束后启动流程
     *
     * @param redirectQuery
     * @return
     */
    @Override
    public Response<Object> updateEnd(RedirectQuery redirectQuery) {

        EamUser user = ThreadLocalContext.get();
        MaintainPlanDetailVO vo = JSON.parseObject(redirectQuery.getFormVariables(), MaintainPlanDetailVO.class);
        String experimentId = vo.getId();

        return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), factory-> {
            MaintainPlanMapper mapper = factory.getMapper(MaintainPlanMapper.class);
            MaintainPlanDetailVO detailVO = mapper.queryDetailById(experimentId, httpServletRequest.getHeader("Language"));

            log.info("experiment:workflow:updateEnd => experimentId={}", experimentId);
            if (Constants.YesOrNo.YES.equals(detailVO.getIsTimer())) {
                return Response.succ(ADOConnection.runTask(user.getCurrDs(), maintainPlanService, "startOrStop", MaintainPlanDetailVO.class, experimentId, true));
            } else {
                detailVO.setStatus(Constants.MAINTAIN_PLAN_STATE.USING);
                mapper.updateById(detailVO);
                MaintainPlanDetailVO maintainPlanDetailVO = ADOConnection.runTask(user.getCurrDs(), issueService, "issue", MaintainPlanDetailVO.class, detailVO.getId());
                return Response.succ(maintainPlanDetailVO);
            }
        },Response.class);

    }

    @Override
    public Response<Object> updateCancel(RedirectQuery redirectQuery) {
        return null;
    }
}
